package cn.dengta.webapp.user.entity.userLevel;

import java.io.Serializable;

import cn.dengta.webapp.user.entity.task.RuleConfig;
import lombok.Getter;
import lombok.Setter;
import me.codeplayer.util.Assert;
import me.codeplayer.util.Cmp;

/**
 * 签到活动的规则配置对象
 */
@Getter
@Setter
public class VipRuleConfig implements RuleConfig {

	private static final long serialVersionUID = 1L;
	//
	protected VipConfigEntry[] entries;

	@Override
	public void validate() {
		for (int i = entries.length - 1; i > 0; ) {
			VipConfigEntry entry = entries[i--];
			entry.validate();
			VipConfigEntry nextEntry = entries[i];
			entry.validate();
			Assert.isTrue(entry.day > nextEntry.day, "VIP天数只能递增！");
			Assert.isTrue(entry.exp >= nextEntry.exp, "等级经验只能递增！");
		}
	}

	@Override
	public void init(RuleConfig config) {
		VipRuleConfig pointConfig = (VipRuleConfig) config;
		this.entries = pointConfig.entries;
	}

	public VipConfigEntry getEntry(Integer times, boolean defaultValue) {
		for (int i = entries.length - 1; i >= 0; i--) {
			if (times >= entries[i].day) {
				return entries[i];
			}
		}
		return defaultValue ? entries[0] : null;
	}

	@Override
	public boolean clearCoin() {
		return true;
	}

	@Override
	public Integer getMin() {
		return entries[0].getExp();
	}

	@Override
	public Integer getMax() {
		return entries[entries.length - 1].getExp();
	}

	/**
	 * VIP经验规则实体
	 */
	@Setter
	@Getter
	public static class VipConfigEntry implements Serializable {

		/** 天数 */
		protected Integer day;
		/** 经验 */
		protected Integer exp;

		public VipConfigEntry(Integer day, Integer exp) {
			this.day = day;
			this.exp = exp;
		}

		public void validate() {
			Assert.isTrue(Cmp.nullToZero(day) > 0, "购买VIP天数必须大于0！");
			Assert.isTrue(Cmp.nullToZero(exp) > 0, "等级经验必须大于0！");
		}

	}

}
